package com.ibm.cloud.envcheck.engine;

import java.io.File;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ibm.cloud.envcheck.bean.ActionResult;
import com.ibm.cloud.envcheck.bean.CheckItem;
import com.ibm.cloud.envcheck.bean.ConfigBean;
import com.ibm.cloud.envcheck.bean.CopyRemoteFileToLocalParam;
import com.ibm.cloud.envcheck.kit.ConfigKit;
import com.ibm.cloud.envcheck.kit.FileUtils;
import com.ibm.cloud.envcheck.kit.PropertyService;
import com.ibm.cloud.envcheck.service.ICheckService;
import com.ibm.cloud.envcheck.service.ServiceHandler;

/**
 * 
 * Check Engine.
 * This engine can help user to check all configuration value which specified by user.
 * @author macky
 *
 */
public class CheckEngine {
	static ServiceHandler handler = null;
	public static void main(String[] args) {
		CheckEngine engine = new CheckEngine();
		handler = new ServiceHandler();
		handler.init("com.ibm.cloud.envcheck.basicservice");
		engine.handle(args);
	}
	
	/**
	 * 
	 * Read configuration from command line.
	 * Read configuration from command line, if no configuration file specified, then
	 * scan all files in configuration folder(./config/)
	 * 
	 */
	private List<File> readConfig(String[]args){
		List<File>files = ConfigKit.scanConfigFiles(args);
		return files;
	}
	
	private void handle(String[]args){
		List<File>files = readConfig(args);
		for(File file:files){
			ConfigBean bean = ConfigKit.readConfig(file);
			List<CheckItem>items = bean.getItems();
			for(CheckItem item:items){
				String type = item.getCheckType();
				ICheckService service = handler.getInstance(108);
				CopyRemoteFileToLocalParam pp= new CopyRemoteFileToLocalParam();
				pp.setHost(bean.getHost());
				pp.setPassword(bean.getPassword());
				pp.setPort(bean.getPort());
				pp.setUsername(bean.getOsAdmin());
				pp.setLocalDirectory(item.getResultStorePath());
				pp.setRemoteFile(item.getRemoteFilePath());
				//copy config file from remote to local
				System.out.println(pp.getHost());
				service.handle(pp);
				//analyze config file
				String remoteFilePath = pp.getRemoteFile();
				String remoteFileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/"));
				String tempPath = pp.getLocalDirectory()+File.separator+remoteFileName;
				File f = new File(tempPath);
				List<ActionResult> ar = PropertyService.checkValues(f,item.getNeedCheckParams());
				List<String>rs = new ArrayList<String>();
				boolean isOk = true;
				for(ActionResult result:ar){
					rs.add("******************************************");
					if(result.isSuccess()){
						rs.add("* Check property key:"+result.getKey() +" success!");
					}else{
						rs.add("* Check property key:"+result.getKey() +" failed!");
						rs.add("* Expect Value is:"+result.getExpectValue());
						rs.add("* Actual Value is:"+result.getActualValue());
					}
					isOk = result.isSuccess() && isOk;
				}
				rs.add("");
				rs.add("Check configuration file completed,The result is:"+isOk);
				Format format = new SimpleDateFormat("yyyyMMdd");
				File resultFile = new File(item.getResultStorePath()+File.separator+format.format(new Date())+f.getName()+"-check-result.txt");
				FileUtils.writeFileFromArray(rs, resultFile);
				
				//delete the temp file
				if(f.exists()){
					f.delete();
				}
			}
		}
		
	}
}
